package com.ruoyi.common.security.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description: 开启mybatisplus自动填充
 * @author: Yuan
 * @create: 2025-02-23 20:24
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
    * @Description: 新增时自动填充字段
    * @Param: [metaObject]
    * @return: void
    * @Author: Yuan
    * @Date: 2025/2/23
    */
    @Override
    public void insertFill(MetaObject metaObject) {
        String username = SecurityUtils.getUsername();
        this.strictInsertFill(metaObject,"createTime",Date.class,new Date());
        this.strictInsertFill(metaObject,"createBy",String.class,username);
    }

    /**
    * @Description: 修改时自动填充字段
    * @Param: [metaObject]
    * @return: void
    * @Author: Yuan
    * @Date: 2025/2/23
    */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject,"updateTime",Date.class,new Date());
        this.strictUpdateFill(metaObject,"updateBy",String.class,SecurityUtils.getUsername());
    }
}
